class Cartasur::Protocol < Cartasur::CartasurModel
  belongs_to :user, :class_name => 'Cartasur::User', :foreign_key => 'users_id'
  belongs_to :objective, :class_name => 'Cartasur::Objective'
  #answers
  has_many :answers, :class_name => 'Cartasur::Answer'

  attr_accessible :objective_id, :users_id, :user, :user_attributes
  accepts_nested_attributes_for :user,
                                :reject_if => :all_blank

  def user_login
    Cartasur::User.find(self.users_id)
  end

  def objective_text
    o = Cartasur::Objective.find(self.objective_id)
    (o.location_name << "| Days: " << o.time_range << " | Months: " << o.months) unless o.nil?
  end

  # summarize objective info, note: no localization!
  def friendly_objective_text
    objective = Cartasur::Objective.find(self.objective_id)
    months = objective.months.split(/,/)
    first_month = months.try(:first)
    last_month = months.try(:last)

    days = objective.days.split(/,/)
    first_day = days.try(:first)
    last_day =  days.try(:last)

    hours = objective.time_range.split(/,/)
    first_hour = hours.try(:first)
    last_hour = hours.try(:last)

    startdate = DateTime.new(2013,first_month.try(:to_i),first_day.try(:to_i),first_hour.try(:to_i))  #year doesn't matter
    enddate = DateTime.new(2013,last_month.try(:to_i),last_day.try(:to_i),last_hour.try(:to_i))  #year doesn't matter

    friendly_text =  objective.location_name
    friendly_text << ": " << first_hour
    friendly_text << "-" << last_hour << "h" unless last_hour == first_hour
    friendly_text <<  ": " <<  startdate.strftime("%a")
    friendly_text << "-" << enddate.strftime("%a")  unless first_day == last_day
    friendly_text << ", " << startdate.strftime("%b")
    friendly_text << "-" << enddate.strftime("%b")   unless first_month == last_month

  end

end